package websearch

import (
	"GoDance/engine"
	"GoDance/utils"
	user "GoDance/webserver/Entity"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
)

//用于存放输入框输入时显示的十条相关内容
func Get_relation() func(c *gin.Context) {
	return func(c *gin.Context) {
		relation := make([]string, 0)
		name := c.Query("search") //输入过来的字段
		//例子
		realTimeSearch := engine.Engine.RealTimeSearch(name)
		relation = realTimeSearch

		//返回数据
		c.JSON(http.StatusOK, gin.H{
			"relation": relation,
		})
	}
}

//用于返回相关搜索  还有  十个数据的id和标题和简介实体类已经在Entity包中定义 以及查询到数据的 条数
func Get_related() func(c *gin.Context) {
	return func(c *gin.Context) {
		related := make([]string, 0)      //存放相关搜索
		results := make([]user.Result, 0) //十个标题和简介实体类
		content := c.Query("search")      //点击查询后输入框中的字段
		not_see := c.Query("not_see")
		index := c.Query("index")
		pagesize := c.Query("pagesize")
		curpage := c.Query("curpage")
		date := c.Query("datetime")

		Param := make(map[string]string)
		Param["index"] = index
		Param["title"] = content
		// Param["content"] = content
		Param["pageSize"] = pagesize
		Param["curPage"] = curpage
		Param["_title"] = not_see
		// Param["_content"] = not_see

		if date != "" {
			if date[0:1] == "~" {
				split := strings.Split(date[1:], " , ")
				fmt.Println(split)
				if len(split) == 2 {
					start, err := utils.IsDateTime(split[0])
					if err != nil {
						start = -1
					}
					end, err := utils.IsDateTime(split[1])
					if err != nil {
						end = -1
					}
					if start != -1 && end != -1 {
						Param[date[0:1]+"date"] = fmt.Sprintf("%v,%v", start, end)
					}
				}

			} else {
				start, err := utils.IsDateTime(date[1:])
				if err != nil {
					start = -1
				}
				Param[date[0:1]+"date"] = fmt.Sprintf("%v", start)
			}
		}

		fmt.Printf("Param : %#v\n", Param)

		related = engine.Engine.RelatedSearch(Param["title"])

		searchResults, _ := engine.Engine.Search(Param)
		// fmt.Printf("%v\n", searchResults.Results)
		for i := 0; i < len(searchResults.Results); i++ {
			var id = searchResults.Results[i]["id"]
			var title = searchResults.Results[i]["title"]
			var introduction string

			contentRune := []rune(searchResults.Results[i]["content"])
			if len(contentRune) > 30 {
				introduction = fmt.Sprintf("%v%v", string(contentRune[0:31]), "...")
			} else {
				introduction = fmt.Sprintf("%v%v", string(contentRune), "...")
			}

			result := user.Result{ID: id, Title: title, Introduction: introduction}
			results = append(results, result)
		}

		//返回数据
		c.JSON(http.StatusOK, gin.H{
			"related":    related,
			"results":    results,
			"totalcount": searchResults.TotalCount,
		})
	}
}

//获取id 返回文章数据
func Get_essay() func(c *gin.Context) {
	return func(c *gin.Context) {
		id := c.PostForm("id")
		indexName := "essay"
		doc, err := engine.Engine.GetDocById(indexName, id)
		if err == nil {
			//返回数据
			c.JSON(http.StatusOK, gin.H{
				"title":   doc["title"],
				"content": doc["content"],
				"author":  doc["author"],
				"date":    doc["date"],
				"id":      doc["id"],
			})
		} else {
			fmt.Printf("err : %v", err)
		}

	}
}
